package commons;
/**
 * This is main class being which is called when the user clicks on sin button
 */
public class CalculatorMarzieh{
    
        public String sinFunc(String str) {
            
            try
            {
                double x = Double.parseDouble(str);  

                double pi =  3.14159265358979323846;
                int no = 5;

                /** change degree to radiant */
                double radians = x;
                if(GUI.degButton.isSelected())
            	radians = x * (pi / 180);
            	
                
                int power = 1;
                double sinX = 0.0;
                for(int i=1; i<=no; i++)
                {
            	double currentTerm = 0.0;
            	if(i%2==0)
            	{
            	    currentTerm = -pow(radians,power)/getFact(power);
            	}
            	else
            	{
            	    currentTerm = pow(radians,power)/getFact(power);        	    
            	}
            	sinX = sinX + currentTerm; 
            	power = power + 2;
                }

                return Double.toString(sinX);        	
            }
            catch(NumberFormatException nf)
            {
        	return "";
            }                        
                       
        }
        

        private long getFact(int n) {
            
            int fact = 1;
            for (int i=1; i<=n; i++)
            {
        	fact = fact * i;
            }
            
            return fact;
        }

        private Double pow(double x, int n) {

                double temp = x;
                if (n == 0) {
                        return 1.0;
                } else {
                        for (int i = 1; i < n; i++) {
                                x *= temp;
                        }
                        return x;
                }
        }


}
